package com.cat.greedyAlgorithm;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/max-chunks-to-make-sorted/description/?envType=problem-list-v2&envId=greedy
 * @create 2025/10/3 19:06
 * @since JDK17
 */

public class Solution47 {
    public int maxChunksToSorted(int[] arr) {
        int n = arr.length, ans = 0, max = 0, min = Integer.MAX_VALUE;
        for (int i = 0, j = 0; j < n; j++) {
            max = Math.max(max, arr[j]);
            min = Math.min(min, arr[j]);
            if (i == min && j == max) {
                ans++;
                min = Integer.MAX_VALUE;
                max = 0;
                i = j + 1;
            }
        }

        return ans;
    }
}
